<?php

namespace app\common\components;

use yii\web\Controller;

class BaseWebController extends Controller
{
	public $enableCsrfValidation =false;

	//获取http的get参数
	public function get( $key ,$defult_val=""){
		return \Yii::$app->request->get($key,$defult_val);
	}
	//获取http的post参数
	public function post($key,$default_val=""){
		return \Yii::$app->request->post($key,$default_val);
	}
	//设置cookie的值
	public function setCookie($name,$value,$explre=0){
		$cookies=\Yii::$app->response->cookies;
		$cookies->add(new \yii\web\Cookie([
				'name'=>$name,
				'value'=>$value,
				'expire'=>$explre
			]));
	}
	//获取cookie
	public function getCookie($name,$default_val='')
	{
		$cookies=\Yii::$app->request->cookies;
		return $cookies->getValue($name,$default_val);
	}
	//删除cookie
	public function removeCookies($name){
		$cookies=\Yii::$app->response->cookies;
		$cookies->remove($name);
	}
	//api统一返回json格式方法
	public function renderJson($data=[],$msg='ok',$code=200){
		header("Content-type:appliction/json");
		return json_encode([
			"code"=>$code,
			"msg"=>$msg,
			"data"=>$data,
			"req_id"=>uniqid()
			]);

	}
	//返回统计js
	public function renderJs($msg,$url){
		return $this->renderPartial("@app/views/common/js",["msg"=>$msg,"url"=>$url]);
	}
}